package com.liy.framework.common.utils;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptUtil {
    private final byte[] DESIV = new byte[]{18, 52, 86, 120, -112, -85, -51, -17};
    private AlgorithmParameterSpec iv = null;
    private Key key = null;
    private String charset = "utf-8";

    public EncryptUtil(String deSkey, String charset) throws Exception {
        DESKeySpec keySpec = new DESKeySpec(deSkey.getBytes(this.charset));
        this.iv = new IvParameterSpec(this.DESIV);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        this.key = keyFactory.generateSecret(keySpec);
    }

    public String encode(String data) throws Exception {
        Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        enCipher.init(1, this.key, this.iv);
        byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encode(pasByte);
    }

    public String decode(String data) throws Exception {
        Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        deCipher.init(2, this.key, this.iv);
        BASE64Decoder base64Decoder = new BASE64Decoder();
        byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
        return new String(pasByte, "UTF-8");
    }

    public static void main(String[] args) {
        try {
            String test = "{'userId': 'TK_C61D6BEDBE07407FADFC3C31BA91FAFA','user234Id': 'TK_C61D6BEDBE07407FADFC3C334341BA91FAFA','user34Id': 'TK_C61D6BEDBE0723434407FADFC3C31BA91FAFA','userId': 'TK_C61D6BEDBE07407FADFC3C31BA91FAFA','user234Id': 'TK_C61D6BEDBE07407FADFC3C334341BA91FAFA','user34Id': 'TK_C61D6BEDBE0723434407FADFC3C31BA91FAFA','userId': 'TK_C61D6BEDBE07407FADFC3C31BA91FAFA','user234Id': 'TK_C61D6BEDBE07407FADFC3C334341BA91FAFA','user34Id': 'TK_C61D6BEDBE0723434407FADFC3C31BA91FAFA','userId': 'TK_C61D6BEDBE07407FADFC3C31BA91FAFA','user234Id': 'TK_C61D6BEDBE07407FADFC3C334341BA91FAFA','user34Id': 'TK_C61D6BEDBE0723434407FADFC3C31BA91FAFA'}";
            String key = "9ba45bfd500642328ec03ad8ef1b6e75";
            EncryptUtil des = new EncryptUtil(key, "utf-8");
            long s = System.currentTimeMillis();
            String en = des.encode(test);
            long e = System.currentTimeMillis() - s;
            System.out.println("加密时间：" + e);
            System.out.println("加密后的字符：" + des.encode(test));
            s = System.currentTimeMillis();
            des.decode(en);
            e = System.currentTimeMillis() - s;
            System.out.println("解密时间：" + e);
            System.out.println("解密后的字符：" + des.decode(des.encode(test)));
        } catch (Exception var9) {
            var9.printStackTrace();
        }

    }
}
